#!/bin/bash
#
# chkconfig: 2345 85 15
# description: Control PostgreSQL 9.0 server process 

### BEGIN INIT INFO
# Provides:          postgres-9.0-openscg
# Required-Start:    
# Required-Stop:     
# Should-Start:  
# Should-Stop:  
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: PostgreSQL 9.0 
# Description: PostgreSQL 9.0 Server       
### END INIT INFO

BREAD_DB=/mnt/db

#Create non-existent postgres user and group
createpguser()
{
    if ! getent group postgres > /dev/null 
    then
       /usr/sbin/groupadd postgres
    fi

    if getent passwd postgres > /dev/null 
    then
      POSTGRES_HOME=`su postgres -c "echo \$HOME"`
      if [ ! -e $POSTGRES_HOME ]; 
      then
       /usr/sbin/usermod -d /opt/postgres/9.0 postgres
      fi
    else
	/usr/sbin/useradd -m -c "PostgreSQL" -d /opt/postgres/9.0 -g postgres postgres
        #Required for OpenSuse platforms
        /usr/sbin/usermod -p "*" postgres
    fi
}

setStartup() 
{
  echo "Configuring PostgreSQL server startup scripts"
  if type "/sbin/chkconfig" &> /dev/null ; then
    /sbin/chkconfig --add postgres-9.0-openscg 
    if [ $? -ne 0 ]; then
        echo "Unable to configure PostgreSQL server boot script"
    fi
  fi
  if type "/usr/sbin/update-rc.d" &> /dev/null ; then
    /usr/sbin/update-rc.d postgres-9.0-openscg defaults 85 15
    if [ $? -ne 0 ]; then
        echo "Unable to configure PostgreSQL server boot script"
    fi
  fi
}

initCluster()
{
  local PASSWORD=$1
  local PORT=$2
  local BOOT=$3
  if [ ! -d ${BREAD_DB} ];
  then
    mkdir -p ${BREAD_DB}
  fi
  createpguser
  chown postgres:postgres ${BREAD_DB}

  touch /tmp/initpass.$$
  chmod 600 /tmp/initpass.$$
  echo $PASSWORD > /tmp/initpass.$$
  chown postgres:postgres /tmp/initpass.$$
  
  if type "/usr/bin/chcon" &> /dev/null ; then
    /usr/bin/chcon -t textrel_shlib_t /opt/postgres/9.0/lib/libedit.so &> /dev/null 
  fi

  su - postgres -c "LD_LIBRARY_PATH=/opt/postgres/9.0/lib:$LD_LIBRARY_PATH /opt/postgres/9.0/bin/initdb  -E utf8 -A md5 --pwfile /tmp/initpass.$$ -D ${BREAD_DB} -U postgres"

  rc=`echo $?`
  rm -f /tmp/initpass.$$
  if [ ! "$rc" == "0" ]; then
    echo " "
    echo "ERROR: Unable to Initialize Postgres DB (rc=$rc)"
    exit 
  fi
  if [ ! -d ${BREAD_DB}/pg_log ];
  then
    mkdir ${BREAD_DB}/pg_log
  fi
  chown postgres:postgres ${BREAD_DB}/pg_log

  #Do some search and replace on postgresql.conf
  if [ -f ${BREAD_DB}/postgresql.conf ];
  then
    su - postgres -c "sed -e \"s@\#listen_addresses = 'localhost'@listen_addresses = '*'@g\" -e \"s@\#port = 5432@port = $PORT@g\" ${BREAD_DB}/postgresql.conf > /tmp/pgconf.$$"
    mv /tmp/pgconf.$$ ${BREAD_DB}/postgresql.conf
    chown postgres:postgres ${BREAD_DB}/postgresql.conf
  fi

  #Set up server startup via chkconfig (for Redhat like distros) and/or update-rc.d (for Ubuntu like distros)
  if [ "$BOOT" == "y" -o "$BOOT" == "Y" ];
  then
    setStartup
  fi
  
}

start()
{
  if [ `id -un` != "postgres" -a `id -u` != "0" ] ;
  then
   echo "ERROR: Must be root or postgres to start PostgreSQL server."
   exit 1
  fi

  #First time initialization
  if [ ! -d ${BREAD_DB} -o ! -f ${BREAD_DB}/postgresql.conf ];
  then
    if [[ `id -u` != "0" ]]; then  
    echo "ERROR: Must be a Superuser to configure PostgreSQL server."
    exit 1
    fi

    echo "PostgreSQL 9.0 Configuration"
    echo "---------------------------------"
    echo "This will initialize PostgreSQL database cluster. The following"
    echo "questions will determine the database server port, superuser password"
    echo " and to start server on system boot. Press <ENTER> to accept defaults."
    echo ""

    local DONE=0
    while [ $DONE -ne 1 ]; do
      echo "Specify PostgreSQL server port [5432]:"
      read  PGPORT
      if [ x"$PGPORT" == x"" ];
      then 
        PGPORT=5432
      fi
      #Valid port is a numeric value between 1024-65535
      echo $PGPORT | egrep '^[0-9]+$' >/dev/null 2>&1
      if [ $? -eq 1 ];
      then
        echo "ERROR: Invalid port number. Valid range is 1024-65535"
        continue
      fi
      if [ $PGPORT -lt 1024 -o $PGPORT -gt 65535 ];
      then
        echo "ERROR: Invalid port number. Valid range is 1024-65535"
        continue
      fi
      DONE=1 
    done

    echo "Specify superuser password [password]:"
    read  -s PGPASSWORD
    echo "Do you want PostgreSQL server to be started on boot (y/n) [y]:"
    read  PGBOOT

    #Assign default values
    if [ x"$PGPASSWORD" == x"" ];
    then 
      PGPASSWORD=password
    fi
    if [ x"$PGBOOT" == x"" ];
    then 
      PGBOOT=y
    fi
    initCluster $PGPASSWORD $PGPORT $PGBOOT

    #Fix for psql dumb terminal issue
    local LD_PRELOAD_VALUE=""
    for libreadline in `find /lib /lib64 -type f -name libreadline.\* 2> /dev/null`
    do
      LD_PRELOAD_VALUE="$libreadline:$LD_PRELOAD_VALUE"
    done
    if [ x"$LD_PRELOAD_VALUE" != x"" ];
    then
      LD_PRELOAD_VALUE="export LD_PRELOAD=$LD_PRELOAD_VALUE"
    fi

    #Dump environment values
cat <<ENVEOF > /opt/postgres/9.0/pg90-openscg.env 
#!/bin/bash
$LD_PRELOAD_VALUE
export PGHOME=/opt/postgres/9.0
export PGDATA=${BREAD_DB}
export PATH=/opt/postgres/9.0/bin:\$PATH
export LD_LIBRARY_PATH=/opt/postgres/9.0/bin:\$LD_LIBRARY_PATH
export PGUSER=postgres
export PGDATABASE=postgres
export PGPORT=$PGPORT
ENVEOF
  fi

  echo "Starting PostgreSQL 9.0: "
  su - postgres -c "LD_LIBRARY_PATH=/opt/postgres/9.0/lib:$LD_LIBRARY_PATH /opt/postgres/9.0/bin/pg_ctl -w start -D ${BREAD_DB} -l \"${BREAD_DB}/pg_log/startup.log\""
	
 if [ $? -eq 0 ];
 then
   echo "PostgreSQL 9.0 started successfully"
   echo "To load this postgres into your environment, source the env file:"
   echo ". /opt/postgres/9.0/pg90-openscg.env"
   exit 0
 else
  echo "The PostgreSQL server start seems to have some problems, please see logs for details."
  exit 1
 fi
}

stop()
{
  if [ `id -un` != "postgres" -a `id -u` != "0" ] ;
  then
   echo "ERROR: Must be root or postgres to stop PostgreSQL server."
   exit 1
  fi
  if [ ! -f ${BREAD_DB}/postgresql.conf ]; then
    echo "PostgreSQL server is not initialized."
    exit 0
  fi
  echo "Stopping PostgreSQL 9.0: "
  su - postgres -c "LD_LIBRARY_PATH=/opt/postgres/9.0/lib:$LD_LIBRARY_PATH /opt/postgres/9.0/bin/pg_ctl stop -m fast -w -D ${BREAD_DB}"
}

reload()
{
  if [ `id -un` != "postgres" -a `id -u` != "0" ] ;
  then
   echo "ERROR: Must be root or postgres to reload PostgreSQL server."
   exit 1
  fi
  if [ ! -f ${BREAD_DB}/postgresql.conf ]; then
    echo "PostgreSQL server is not initialized."
    exit 1
  fi
  echo "Reloading PostgreSQL 9.0: "
  su - postgres -c "LD_LIBRARY_PATH=/opt/postgres/9.0/lib:$LD_LIBRARY_PATH /opt/postgres/9.0/bin/pg_ctl reload -D ${BREAD_DB}"
}

restart() 
{
  if [ `id -un` != "postgres" -a `id -u` != "0" ] ;
  then
   echo "ERROR: Must be root or postgres to restart PostgreSQL server."
   exit 1
  fi
  if [ ! -f ${BREAD_DB}/postgresql.conf ];
  then
    echo "PostgreSQL server is not initialized."
    exit 1
  fi
  if [ -f "${BREAD_DB}/postmaster.pid" ]; then
    stop
    sleep 3
    start
  else
    start
  fi
}
# Determine arguments passed to script
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  reload)
        reload
        ;;
  restart)
        restart
        ;;
  status)
        if [ `id -un` != "postgres" -a `id -u` != "0" ] ;
        then
          echo "ERROR: Must be root or postgres to get PostgreSQL server status."
          exit 1
        fi
        if [ ! -f ${BREAD_DB}/postgresql.conf ];
        then
          echo "PostgreSQL server is not initialized."
          exit 1
        fi
        su - postgres -c "LD_LIBRARY_PATH=/opt/postgres/9.0/lib:$LD_LIBRARY_PATH /opt/postgres/9.0/bin/pg_ctl status -D ${BREAD_DB}"
        ;;
  *)
        echo "Usage: $0 {start|stop|restart|reload|status}"
        exit 1
esac

